package com.ruoyi.web.controller.minglvmini;

import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

@RestController
//@RequestMapping("/upload")
public class ApiImagesController {


    @GetMapping(value = "/files/{type}/{fileName}", produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_GIF_VALUE})
    @ResponseBody
    public byte[] getImages(@PathVariable("type") String type, @PathVariable("fileName") String fileName) throws Exception {
        String defaultBaseDir = FileUploadUtils.getDefaultBaseDir();
        String path = defaultBaseDir + File.separator + "files" + File.separator + type + File.separator + fileName;
        File file = new File(path);
        try (FileInputStream inputStream = new FileInputStream(file)) {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes, 0, inputStream.available());
            return bytes;
        }
    }

    @GetMapping(value = "/upload/{type}/{date}/{fileName}",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE,MediaType.IMAGE_GIF_VALUE})
    @ResponseBody
    public byte[] getImages(@PathVariable("type") String type,@PathVariable("date") String date, @PathVariable("fileName") String fileName) throws Exception{
        String defaultBaseDir = FileUploadUtils.getDefaultBaseDir();
        String path  = defaultBaseDir +  File.separator + "upload" + File.separator + type + File.separator + date + File.separator + fileName;
        File file = new File(path);
        try(FileInputStream inputStream = new FileInputStream(file)){
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes,0,inputStream.available());
            return bytes;
        }
    }

    @GetMapping(value = "/uploads/{type}/{date}/{fileName}",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE,MediaType.IMAGE_GIF_VALUE})
    @ResponseBody
    public byte[] getImagess(@PathVariable("type") String type,@PathVariable("date") String date, @PathVariable("fileName") String fileName) throws Exception{
        String defaultBaseDir = FileUploadUtils.getDefaultBaseDir();
        String path  = defaultBaseDir +  File.separator + "uploads" + File.separator + type + File.separator + date + File.separator + fileName;
        File file = new File(path);
        try(FileInputStream inputStream = new FileInputStream(file)){
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes,0,inputStream.available());
            return bytes;
        }
    }

    @GetMapping(value = "/static/{type}/{fileName}",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE,MediaType.IMAGE_GIF_VALUE})
    @ResponseBody
    public byte[] getStaticImages(@PathVariable("type") String type, @PathVariable("fileName") String fileName) throws Exception{
        String defaultBaseDir = FileUploadUtils.getDefaultBaseDir();
        String path  = defaultBaseDir +  File.separator + "static" + File.separator + type +  File.separator + fileName;
        File file = new File(path);
        try(FileInputStream inputStream = new FileInputStream(file)){
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes,0,inputStream.available());
            return bytes;
        }
    }
}
